package com.yitiao.data_algorithms.leetcode.editor.cn;

import com.yitiao.data_algorithms.leetcode.editor.cn.base.ListNode;
import com.yitiao.data_algorithms.leetcode.editor.cn.base.TreeNode;

class ReverseStringIi {
    // 2022-08-23 10:52:25
    public static void main(String[] args) {
        Solution solution = new ReverseStringIi().new Solution();
        String abcdefg = solution.reverseStr("abcdefg", 2);
        System.out.println(abcdefg);

    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public String reverseStr(String s, int k) {
            char[] chars = s.toCharArray();
            int count = s.length() / (2 * k) + 1;
            int times = 0;
            while (times < count) {
                int head = times * 2 * k;
                int tail = Math.min(times * 2 * k + k - 1, s.length() - 1);
                while (head < tail) {
                    char c = chars[head];
                    chars[head] = chars[tail];
                    chars[tail] = c;
                    head++;
                    tail--;
                }
                times++;
            }
            return String.copyValueOf(chars);

        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}